package com.atguigu.DoubleLinkedList;

/**
 * description:
 * Created by 才睡醒 on 2021-06-08
 */
public class DoubleLinkedListDemo {
    public static void main(String[] args) {
        HeroNode hero1 = new HeroNode(1, "宋江", "及时雨");
        HeroNode hero2 = new HeroNode(3, "卢俊义", "玉麒麟");
        HeroNode hero3 = new HeroNode(5, "吴用", "智多星");
        HeroNode hero4 = new HeroNode(7, "林冲", "豹子头");
        HeroNode hero5 = new HeroNode(2, "秦明", "霹雳火");

        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
        doubleLinkedList.addOrderBy(hero1);
        doubleLinkedList.addOrderBy(hero3);
        doubleLinkedList.addOrderBy(hero2);
        doubleLinkedList.addOrderBy(hero5);
        doubleLinkedList.addOrderBy(hero4);

        doubleLinkedList.list();

    }
}

class DoubleLinkedList {

    HeroNode head = new HeroNode(0, "", "");


    public void add(HeroNode heroNode) {
        //因为head头节点不能动，因此我们需要一个辅助遍历temp
        HeroNode temp = head;
        //遍历列表，找到最后
        while (true) {
            //如果temp.next等于空，相当于找打最后一个节点
            if (temp.next == null) {
                break;
            }
            //如果temp。next不为空，temp后移
            temp = temp.next;
        }
        //当退出while循环时，temp指向链表的最后
        //将最后这个节点的next指向新的节点
        //形成一个双向链表
        temp.next = heroNode;
        heroNode.pre = temp;
    }


    //9.根据noid修改内容
    public void modify(HeroNode newHeronode) {
        //判断链表是否为空
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        HeroNode temp = head;
        boolean flag = false;
        //遍历链表
        while (true) {
            if (temp == null) { //temp.next等于空表示已经遍历到链表最后
                break;
            }
            if (temp.no == newHeronode.no) {
                flag = true;
                break;
            }

            temp = temp.next;//元素后移
        }
        //根据flag判断是否找到要修改的节点
        if (flag) {
            temp.name = newHeronode.name;
            temp.nickname = newHeronode.nickname;
        } else {
            System.out.println("没有找到编号");
        }
    }


    //删除节点
    public void delete(HeroNode heroNode) {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        //定义辅助变量
        HeroNode temp = head.next;
        //定义旗帜标签
        boolean flag = false;
        while (true) {
            if (temp == null) {
                break;
            }
            if (temp.no == heroNode.no) {
                flag = true;
                break;
            }
            //节点后移
            temp = temp.next;
        }
        //删除节点
        if (flag) {
            temp.pre.next = temp.next;
            if (temp.next != null) {
                temp.next.pre = temp.pre;
            }
        }
    }

    //8.按照顺序添加
    public void addOrderBy(HeroNode heroNode) {
        //因为头节点不能动，需要使用辅助变量
        //因为单链表，因为我们找的temp是位于添加位置的前一个节点，否则插入不了
        HeroNode temp = head;
        //flag 标志添加的编号是否存在，默认为false
        boolean flag1 = false;
        boolean flag2 = false;
        //遍历元素
        while (true) {
            //如果temp.next等于空，相当于找打最后一个节点
            if (temp.next == null) {
                flag2 = true;
                break;
            }
            if (temp.next.no > heroNode.no) { //位置找到，就在temp的后面插入
                break;
            } else if (temp.next.no == heroNode.no) {
                flag1 = true;//说明编号存在
                break;
            }
            temp = temp.next;//元素一定要后移
        }
        //判断flag的值
        if (flag1) {
            System.out.printf("准备插入的英雄的编号%d已经存在了\n", heroNode.no);
        } else if (flag2) {
            heroNode.next = temp.next;
            heroNode.pre = temp;
            temp.next = heroNode;
        } else {
            heroNode.next = temp.next;
            temp.next.pre = heroNode;
            temp.next = heroNode;
            heroNode.pre = temp;
        }

    }

    public void list() {
        //判断链表是否为空
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        HeroNode temp = head.next;
        while (true) {
            //判断是否到链表最后
            if (temp == null) {
                break;
            }
            //输出节点信息
            System.out.println(temp);
            //将temp后移
            temp = temp.next;
        }
    }

}


//定义HeroNode,每个HeroNode 对象都是一个节点
class HeroNode {
    public int no;
    public String name;
    public String nickname;
    public HeroNode next; //指向下一个节点
    public HeroNode pre; //指向前一个节点

    //构造器
    public HeroNode(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    //重写toString
    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +

                '}';
    }
}

